package com.yiqi.zhiyuan.feature.msg.presenter;

import com.suozhang.framework.entity.bo.PageData;
import com.yiqi.zhiyuan.entity.vo.MessageTypeVO;
import com.yiqi.zhiyuan.entity.vo.MsgVO;
import com.yiqi.zhiyuan.feature.msg.contract.MessageContract;
import com.yiqi.zhiyuan.feature.msg.dependencies.message.DaggerMessagePresenterComponent;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @ProjectName: ZhiYuan
 * @Description: java类作用描述
 * @Author: lijuwen
 * @CreateDate: 2019/3/14 10:54
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/3/14 10:54
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class MessagePresenter implements MessageContract.Presenter {

    private MessageContract.View mView;

    @Inject
    MessageContract.Model mModel;


    public MessagePresenter(MessageContract.View mView) {
        this.mView = mView;
        DaggerMessagePresenterComponent.create().inject(this);
    }



    @Override
    public void start() {

    }

    @Override
    public void doNotification() {
      mModel.doNotification()
              .compose(mView.bindToLife())
              .subscribe(new Observer<MsgVO>() {
                  @Override
                  public void onSubscribe(Disposable d) {
                      mView.showLoading(d);
                  }

                  @Override
                  public void onNext(MsgVO list) {
                          mView.dismissLoading();
                          mView.showMessageSuccess(list);
                  }

                  @Override
                  public void onError(Throwable e) {
                      mView.dismissLoading();
                      mView.showMessageListError(e.getMessage());
                  }

                  @Override
                  public void onComplete() {
                      mView.dismissLoading();
                  }
              });
    }

    @Override
    public void doMessageList(int type, int page) {
        mModel.doMessageList(type,page)
                .compose(mView.bindToLife())
                .subscribe(new Observer<PageData<MessageTypeVO>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(PageData<MessageTypeVO> list) {
                        mView.dismissLoading();
                        if (list!=null&&list.getList()!=null){
                            if (list.getList().size()>0){
                                mView.showMessageListSuccess(list.getList());
                            }else {
                                mView.showMessageListEmpty();
                            }
                        }else {
                            mView.showMessageListEmpty();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showMessageListError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void doMessageMoreList(int type, int page) { mModel.doMessageList(type,page)
            .compose(mView.bindToLife())
            .subscribe(new Observer<PageData<MessageTypeVO>>() {
                @Override
                public void onSubscribe(Disposable d) {

                }

                @Override
                public void onNext(PageData<MessageTypeVO> list) {
                    mView.dismissLoading();
                    mView.showMessageListMoreSuccess(list.getList());
                }

                @Override
                public void onError(Throwable e) {
                    mView.dismissLoading();
                    mView.showMessageListMoreError(e.getMessage());

                }

                @Override
                public void onComplete() {
                    mView.dismissLoading();
                }
            });
    }
}
